package HtmlHelper

import (
	"bytes"
	"encoding/json"
	"gitee.com/tym_hmm/go-helper/StringHelper"
	"strings"
)

//html字符串处理(转义与反转义)
//使用json
//序列化html
func HtmlSerialization(htmlStr string) (string, error) {
	if len(strings.Trim(htmlStr, "")) == 0 {
		return "", nil
	}
	htmlStr = StringHelper.EmojiEnCode(htmlStr)
	//转义描述中的html
	var buffer bytes.Buffer
	encode := json.NewEncoder(&buffer)
	encode.SetEscapeHTML(true)
	encodeErr := encode.Encode(htmlStr)
	if encodeErr != nil {
		return "", encodeErr
	}
	return buffer.String(), nil
}

func HtmlUnSerialization(encodeStr string) (string, error) {
	if len(strings.Trim(encodeStr, "")) == 0 {
		return "", nil
	}
	var decodeBuffer string
	deeCodeErr := json.NewDecoder(strings.NewReader(encodeStr)).Decode(&decodeBuffer)
	if deeCodeErr != nil {
		return "", deeCodeErr
	}
	decodeBuffer = StringHelper.EmojiDecode(decodeBuffer)
	return decodeBuffer, nil
}
